package MetroLab.Train;

import MetroLab.Path.PosPoint;
import MetroLab.Utils.*;
import javax.media.j3d.*;
import javax.vecmath.*;

/**
 *
 * @author plus
 */
public class Carriage extends TransformGroup {

    private Train MyTrain;
    private Group VisualCarriage;
    private double offsetD;
    private Bogie[] Bogies;
    private Pantograph MyPant;
    
    PosPoint currentPos=new PosPoint();
    
    public Carriage(Train MyTrain, double offsetD, Point2d BogiesOffsetD, String ModelFileKey,String BogiesFileKey) {
        this.MyTrain = MyTrain;
        this.offsetD = offsetD;
        this.Bogies = Bogies;
        VisualCarriage = ObjectFactory.createObject(ModelFileKey);
        Transform3D trans = new Transform3D();
        trans.set(new Vector3d(offsetD, 0d, 0d));
        this.setTransform(trans);
        this.setCapability(this.ALLOW_TRANSFORM_WRITE);
        this.addChild(VisualCarriage);
        MyTrain.getTrainGroup().addChild(this);
        initBogie(BogiesOffsetD,BogiesFileKey);
        //MyTrain.getTrainGroup().addChild(Bogies[0]);
        //MyTrain.getTrainGroup().addChild(Bogies[1]);
        RefreshPosition();
    }
    
    private void initBogie(Point2d BogiesOffsetD,String BogiesFileKey){
        Bogies=new Bogie[]{new Bogie(MyTrain,offsetD+BogiesOffsetD.x,BogiesFileKey),
                           new Bogie(MyTrain,offsetD+BogiesOffsetD.y,BogiesFileKey)};
    }
    
    public PosPoint RefreshPosition(){
        currentPos.d=MyTrain.D+offsetD;
        //currentPos=MyTrain.MyRail.makePath(currentPos);
        PosPoint posBogieA, posBogieB;
        posBogieA=Bogies[0].RefreshPosition();
        posBogieB=Bogies[1].RefreshPosition();
        currentPos.AngH=0.5*(posBogieA.AngH+posBogieB.AngH);
        
        double deltaZ=posBogieB.pos.z-posBogieA.pos.z,
                deltaY=posBogieB.pos.y-posBogieA.pos.y,
                deltaX=posBogieB.pos.x-posBogieA.pos.x,
                deltaD=Bogies[1].offsetD-Bogies[0].offsetD,
                step=(Bogies[1].offsetD-offsetD)/deltaD;
        currentPos.pos=new Point3d(posBogieB.pos.x-deltaX*step,
                posBogieB.pos.y-deltaY*step,
                posBogieB.pos.z-deltaZ*step);
        //currentPos.AngV=0.5*(posBogieA.AngV+posBogieB.AngV);
        if (Math.abs(deltaX)<0.01){
            if (deltaZ>0)
                currentPos.AngV=-Math.PI/2;
            else
                currentPos.AngV= Math.PI/2;
        }else
            currentPos.AngV=Math.atan2(-deltaZ,deltaX);
        
        double zfix=Math.atan2(deltaY, deltaD);
        setCarriageTransform(currentPos,zfix);
        
        return currentPos;
    }
    
    private void setCarriageTransform(PosPoint p,double Zfix){
        Transform3D transH=new Transform3D();
        transH.rotX(p.AngH);
        Transform3D transV=new Transform3D();
        transV.rotY(p.AngV);
        Transform3D transZ=new Transform3D();
        transZ.rotZ(Zfix);
        Transform3D transT=new Transform3D();
        transT.set(new Vector3d(p.pos.x,p.pos.y,p.pos.z));
        
        transT.mul(transV);
        transT.mul(transZ);
        transT.mul(transH);
        
        this.setTransform(transT);
    }
}
